home *** CD-ROM | disk | FTP | other *** search
/ GFX Sensations 1 / Graphic Sensations - Volume 1.iso / tools / amiga / 3d_tools / irit40s.lha / Irit / prsr_lib / ami_clnt.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-30  |  5.9 KB  |  208 lines

  1. /*****************************************************************************
  2. *   Routines to handle I/O of objects through exec message ports             *
  3. *   Client side.                                                             *
  4. *                                                                            *
  5. * Written by:  Kriton Kyrimis                                                *
  6. *        and   Gershon Elber                        Ver 0.2, December 1993.  *
  7. *****************************************************************************/
  8.  
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <exec/types.h>
  12. #include <exec/ports.h>
  13. #include <exec/memory.h>
  14.  
  15. #ifdef __SASC
  16. #include <dos.h>
  17. #include <proto/dos.h>
  18. #include <proto/exec.h>
  19. #endif
  20.  
  21. #ifdef __GNUC__
  22. #include <dos/dos.h>
  23. #include <dos/var.h>
  24. #include <inline/dos.h>
  25. #include <inline/exec.h>
  26. #endif
  27.  
  28. #include "irit_sm.h"
  29. #include "irit_soc.h"
  30.  
  31. #include "amiga.h"
  32.  
  33. static struct MsgPort *port = NULL;
  34. static LONG oldpri;
  35. static int Active = FALSE;
  36. static int GlblBinaryIPC = FALSE;
  37. static int GlblEchoInput = FALSE;
  38. static char GlblClientUnReadChar = 0;
  39.  
  40. static int SocReadObjPrefix(void);
  41.  
  42. /*****************************************************************************
  43. *  Opens the port. Returns TRUE if succesful.                                *
  44. *****************************************************************************/
  45. int SocClientCreateSocket(void)
  46. {
  47.     int len;
  48.     char *name, buf[4];
  49.  
  50.     GlblBinaryIPC = getenv("IRIT_BIN_IPC") != NULL;
  51.     len = GetVar(SERVER_VAR, buf, sizeof(buf), GVF_GLOBAL_ONLY);
  52.     if (len < 0) {
  53.     return NULL;
  54.     }
  55.     DeleteVar(SERVER_VAR, GVF_GLOBAL_ONLY);
  56.  
  57.     name = getenv("IRIT_SERVER_PORT");
  58.     if (!name) {
  59.         name = IRIT_SERVER_PORT;
  60.     }
  61.     Forbid();
  62.     port = FindPort(name);
  63.     Permit();
  64.     if (port) {
  65.     Active = TRUE;
  66.     buf[0] = '\0';
  67.     SetVar(CLIENT_VAR, buf, -1, GVF_GLOBAL_ONLY);
  68.     /* This beast busy-waits! Reduce its priority so that we don't hog
  69.        other tasks, namely irit. */
  70.     oldpri = SetTaskPri(FindTask(0L), -1);
  71.     return TRUE;
  72.     } else {
  73.     return FALSE;
  74.     }
  75. }
  76.  
  77. /*****************************************************************************
  78. *  Close the port.                                                           *
  79. *****************************************************************************/
  80. void SocClientCloseSocket(void)
  81. {
  82.     /* Nothing to do on our side. */
  83.     Active = FALSE;
  84.     SetTaskPri(FindTask(0L), oldpri);
  85. }
  86.  
  87. /*****************************************************************************
  88. *  Set echo printing of read input.                                          *
  89. *****************************************************************************/
  90. void SocClientEchoInput(int EchoInput)
  91. {
  92.     GlblEchoInput = EchoInput;
  93. }
  94.  
  95. /*****************************************************************************
  96. * Attempt to read (non blocking) from socket an object.                      *
  97. * If read is successful the object is returned, otherwise NULL is returned.  *
  98. *****************************************************************************/
  99. IPObjectStruct *SocClientReadOneObject(void)
  100. {
  101.     char *ErrorMsg;
  102.     IPObjectStruct *PObj;
  103.  
  104.     if (Active && SocReadObjPrefix()) {
  105.         IritPrsrSetReadOneObject(TRUE);
  106.  
  107.         IritPrsrReadSocket(TRUE);
  108.         if (GlblBinaryIPC) {
  109.             PObj = IritPrsrGetBinObject(NULL);
  110.         }
  111.         else { 
  112.             PObj = IritPrsrGetObjects(NULL);
  113.         }
  114.         IritPrsrReadSocket(FALSE);
  115.     } else {
  116.         PObj = NULL;
  117.     }
  118.  
  119.     if (IritPrsrParseError(&ErrorMsg)) {
  120.         fprintf(stderr, "Socket: %s\n", ErrorMsg);
  121.     }
  122.  
  123.     return PObj;
  124. }
  125.  
  126. /*****************************************************************************
  127. *  Unget once char from client port                                          *
  128. *****************************************************************************/
  129. void SocClientUnReadChar(char c)
  130. {
  131.     GlblClientUnReadChar = c;
  132. }
  133.  
  134. /*****************************************************************************
  135. *  Non blocking read of a single char. Returns EOF if no data is found       *
  136. *****************************************************************************/
  137. int SocClientReadCharNonBlock(void)
  138. {
  139.     struct IritMessage *msg;
  140.     int c;
  141.  
  142.     static int BufferSize = 0, BufferPtr = 0;
  143.     static unsigned char Buffer[MSGSIZE];
  144.  
  145.     if (GlblClientUnReadChar != 0) {
  146.     c = GlblClientUnReadChar;
  147.     GlblClientUnReadChar = 0;
  148.     } else {
  149.         if (BufferPtr < BufferSize) {
  150.             c = Buffer[BufferPtr++];
  151.         } else {
  152.             msg = (struct IritMessage *)GetMsg(port);
  153.         if (msg) {
  154.         BufferSize = msg->nbytes;
  155.         memcpy(Buffer, msg->txt, BufferSize);
  156.         ReplyMsg((struct Message *)msg);
  157.             if (GlblEchoInput) {
  158.                 int i;
  159.                 unsigned char *p = Buffer;
  160.  
  161.                 if (GlblBinaryIPC) {
  162.                     for (i = 0; i < BufferSize; i++) {
  163.                         if (i % 16 == 0)
  164.                             printf("\n%04x: ", i);
  165.                         printf("%02x ", *p++);
  166.                     }
  167.                     printf("\n");
  168.                 }
  169.                 else
  170.                     for (i = 0; i < BufferSize; i++) {
  171.                         char tmp = *p++;
  172.                         putc(tmp, stdout);
  173.                     }
  174.             }
  175.         BufferPtr = 0;
  176.         c = Buffer[BufferPtr++];
  177.         } else {
  178.             c = EOF;
  179.         }
  180.         }
  181.     }
  182.     return c;
  183. }
  184.  
  185. /*****************************************************************************
  186. * Get a single line for syncronization purposes that will prefix an object.  *
  187. *   Returns TRUE if prefix found, FALSE otherwise.                           *
  188. *****************************************************************************/
  189. static int SocReadObjPrefix(void)
  190. {
  191.     if (GlblBinaryIPC) {
  192.         int c;
  193.  
  194.         if ((c = SocClientReadCharNonBlock()) != EOF) {
  195.             SocClientUnReadChar(c);
  196.             return TRUE;
  197.         }
  198.     }
  199.     else {
  200.         if (SocClientReadCharNonBlock() == '[') {
  201.         SocClientUnReadChar('[');
  202.         return TRUE;
  203.         }
  204.     }
  205.  
  206.     return FALSE;
  207. }
  208.